package com.cskt.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cskt.common.condition.AddUserLinkUserCondition;
import com.cskt.common.condition.SearchUserLinkUserCondition;
import com.cskt.common.constants.ErrorCodeEnum;
import com.cskt.common.vo.ReturnResult;
import com.cskt.entity.UserLinkUser;


import com.cskt.itripbiz.service.UserLinkUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @author mo
 * @Description: 用户信息控制器
 * @date 2020-10-08 17:22
 */
@Api(tags = "用户信息控制器")
@RestController
@RequestMapping(value = "/api/userinfo")
public class UserInfoController {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private UserLinkUserService userLinkUserService;

    @ApiOperation(value = "查询常用联系人接口", httpMethod = "POST",
            protocols = "HTTP", produces = "application/json",
            response = ReturnResult.class, notes = "查询常用联系人信息(可根据联系人姓名进行模糊查询)" +
            "<p>若不根据联系人姓名进行查询，不输入参数即可 | 若根据联系人姓名进行查询，须进行相应的入参，比如：{\"linkUserName\":\"张三\"}</p>")
        @RequestMapping(value = "/queryuserlinkuser", method = RequestMethod.POST)
        @ResponseBody
    public ReturnResult queryUserLinkUser(@RequestBody SearchUserLinkUserCondition condition, HttpServletRequest request){
        //先验证当前用户是否登录
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            // token 为空
            return ReturnResult.error(ErrorCodeEnum.AUTH_TOKEN_IS_EMPTY);
        }

        LambdaQueryWrapper<UserLinkUser> lambdaQueryWrapper = new QueryWrapper<UserLinkUser>().lambda();
        lambdaQueryWrapper.like(StringUtils.hasText(condition.getLinkUserName()),UserLinkUser::getLinkUserName,condition.getLinkUserName());
        List<UserLinkUser> userLinkUsers = userLinkUserService.list(lambdaQueryWrapper);
        return ReturnResult.ok(userLinkUsers);
    }

    public ReturnResult addUserLinkUser(@RequestBody AddUserLinkUserCondition condition, HttpServletRequest request){
        //先验证当前用户是否登录
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
        // token 为空
            return ReturnResult.error(ErrorCodeEnum.AUTH_TOKEN_IS_EMPTY);
        }
        String loginUserString = stringRedisTemplate.opsForValue().get(token);
        if (StringUtils.isEmpty(loginUserString)) {
        //token 失效
            return ReturnResult.error(ErrorCodeEnum.AUTH_TOKEN_INVALID);
        }
        //条件不能为空
        if (StringUtils.isEmpty(condition)) {
            return ReturnResult.error(ErrorCodeEnum.BIZ_PARAMETER_IS_EMPTY);
        }
        boolean saveResult = userLinkUserService.saveUserLinkUser(condition);
        if (!saveResult) {
            return ReturnResult.error(ErrorCodeEnum.SYSTEM_EXECUTION_ERROR);
        }
        return ReturnResult.ok();

    }
}
